require('es6-promise').polyfill();

import axios from 'axios'
import _ from 'lodash'

export function loadUnits(data) {
  return {
    type: 'LOAD_UNITS',
    data
  }
}

//物业管家多选显示物业列表
export function loadUnitsLists(data) {
  return {
    type: 'LOAD_SYS_USER_UNITS_LIST',
    data
  }
}

export function loadUnitsDetail(data) {
  return {
    type: 'LOAD_UNIT_DETAIL',
    data
  }
}


//物业管家列表 SysUserListReducer
export function loadUsers(data) {
  return {
    type: 'LOAD_SYS_USER_UNIT',
    data
  }
}

function errorAction(error) {
  return {
    type: 'ERROR_UNITS',
    error
  }
}

export function apiMsgAction(msg) {
  return {
    type: 'API_MSG',
    msg
  }
}

export function loadUnitsAsync(data) {
  return function(dispatch) {
    return axios.post('/api/units/', data).then(
      resp => dispatch(loadUnits(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    )
  }
}

export function loadUnitsLimitAsync(data) {

  return function(dispatch) {
    return axios.post('/api/units/query', data).then(
      resp => dispatch(loadUnits(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    )
  }
}

export function loadSysUserUnitsLimitAsync(data) {

  return function(dispatch) {
    return axios.post('/api/units/query', data).then(
      resp => dispatch(loadUnitsLists(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    )
  }
}

export function loadUnitsDetailAsync(data) {

  return function(dispatch) {
    return axios.post('/api/units/queryUserBindByUnit', data)
    .then((resp) => {
        dispatch(loadUnitsDetail(resp.data))
    })
    .catch((error) => {
      dispatch(apiMsgAction(error.data.errMsg))
    })
  }
}

export function loadUsersAsync(data) {
  return function(dispatch) {
    return axios.post('/api/units/querySysUser', data)
    .then((resp) => {
        dispatch(loadUsers(resp.data))
    })
    .catch((error) => {
      var hasOne = false
      if (data) {
        if (data.sys_role_user && data.sys_role_user.length > 0) {
           data.sys_role_user.forEach(function(roleUser) {
            if (roleUser.role) {
              if (roleUser.role.name == "物业管家" || roleUser.role.name == "管家客服") {
                hasOne = true
              }
            }
          })
        }
      }
      if (!hasOne) {
        dispatch(apiMsgAction(error.data.errMsg))
      }

    })
  }
}

// 提交物业管家
export function submitUsersAsync(data) {
  return function(dispatch) {
    return axios.post('/api/units/SubmitSysUser', data).then((resp)=> {
      dispatch(loadUnitsLimitAsync(data))
      dispatch(loadSysUserUnitsLimitAsync({
        limit: 6
      }))
    })
    .catch((error)=> {
      dispatch(apiMsgAction(error.data.errMsg))
    })
  }
}

export function createUnitsAsync(data) {
  return function(dispatch) {
    return axios.post('/api/units/create', data).then(
      resp => dispatch(loadUnitsLimitAsync(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    )
  }
}

export function updateUnitsAsync(data) {
  return function(dispatch) {
    return axios.post('/api/units/update', data).then(
      resp => dispatch(loadUnitsLimitAsync({
        offset: data.offset,
        limit: data.limit,
        unit_number: data.search
      })),
      error => dispatch(apiMsgAction(error.data.errMsg))
    )
  }
}

export function deleteUnitsAsync(data) {
  return function(dispatch) {
    return axios.post('/api/units/delete', data).then(
      resp => dispatch(loadUnitsLimitAsync({
        offset: data.offset,
        limit: data.limit,
        unit_number: data.search
      })),
      error => dispatch(apiMsgAction(error.data.errMsg))
    )
  }
}
